#include "host.h"

#include <pthread.h>

using namespace host;


static void* Main(void* data)
{
    OSThread *self = (OSThread*)data;

    while (self->m_bRunning)
    {
        bool ret = self->callback(self->m_pUser);
        if (ret == false)
            break;
    }

    return NULL;
}

host::OSThread::OSThread()
{
    m_bRunning = false;
    m_pUser = NULL;
    callback = NULL;
}

host::OSThread::~OSThread()
{
    m_bRunning = false;
    free(m_hThread);
}

void host::OSThread::Load(TaskCB cb, void* priv)
{
    m_pUser = priv;
    callback = cb;

    m_bRunning = true;

    m_hThread = malloc(sizeof(pthread_t));
    pthread_create((pthread_t*)m_hThread, NULL, Main, this);
}

void host::OSThread::Trem()
{
    m_bRunning = false;
}
